package com.system.controller;

import com.common.annotation.MyLog;
import com.common.constants.Constant;
import com.common.controller.BaseController;
import com.system.entity.File;
import com.system.service.FileService;
import com.system.vo.request.FilePageReqVO;
import com.system.vo.response.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.Valid;
import java.util.List;
import com.common.res.DataResult;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.Valid;
import java.util.List;

/**
* @author XueHeng
* @since 2020-09-17
*/
@RestController
@RequestMapping("api")
@Api(tags = "文件系统")
public class FileController extends BaseController {

    @Autowired
    private FileService fileService;

    @PostMapping("/files")
    @ApiOperation(value = "我的文件列表")
    @MyLog(title = "文件系统",action = "获取文件列表")
    @RequiresPermissions("sys:file:list")
    public DataResult<PageVO<File>> getAllFile(@RequestBody @Valid FilePageReqVO vo){
        DataResult result =DataResult.success();
        result.setData(this.fileService.selectAll(vo));
        return result;
    }

    @GetMapping("/file/{id}")
    @ApiOperation(value = "文件下载")
    @MyLog(title = "文件系统",action = "文件下载")
    @RequiresPermissions("sys:file:download")
    public void download(@PathVariable("id") String id, HttpServletResponse response){
        this.fileService.download(response,id);
    }

    @PostMapping("/file")
    @ApiOperation(value = "文件上传")
    @MyLog(title = "文件系统",action = "文件上传")
    //@RequiresPermissions("sys:file:upload")
    public DataResult upload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        DataResult result =DataResult.success();
        String userId = getUserId(request);
        Integer type = request.getIntHeader(Constant.FILE_TYPE);
        result.setData(this.fileService.upload(file,userId,type));
        return result;
    }

}
